import java.util.HashMap;
import java.util.Map;

public class ShoppingCart {
    private Map<Product, Integer> items;

    public ShoppingCart() {
        this.items = new HashMap<>();
    }

    public void addProduct(Product product, int quantity) {
        items.put(product, items.getOrDefault(product, 0) + quantity);
    }

    public void removeProduct(Product product) {
        items.remove(product);
    }

    public void updateProductQuantity(Product product, int quantity) {
        if (quantity <= 0) {
            items.remove(product);
        } else {
            items.put(product, quantity);
        }
    }

    public Map<Product, Integer> getItems() {
        return items;
    }

    public double getTotalAmount() {
        return items.entrySet().stream().mapToDouble(entry -> entry.getKey().getRetailPrice() * entry.getValue()).sum();
    }

    public void clearCart() {
        items.clear();
    }

    @Override
    public String toString() {
        StringBuilder cartContents = new StringBuilder("购物车内容:\n");
        for (Map.Entry<Product, Integer> entry : items.entrySet()) {
            cartContents.append(entry.getKey().getName()).append(" x ").append(entry.getValue()).append(" - ¥").append(entry.getKey().getRetailPrice() * entry.getValue()).append("\n");
        }
        return cartContents.toString();
    }
}